package com.wzapp.sitposture.usercenter.endpoint;

import com.wzapp.sitposture.common.constant.PayConst;
import com.wzapp.sitposture.common.util.DateUtil;
import com.wzapp.sitposture.common.util.ToolUtil;
import com.wzapp.sitposture.eshop.service.OrderService;
import com.wzapp.sitposture.eshop.view.UserOrderDTO;
import com.wzapp.sitposture.usercenter.entity.UserIncome;
import com.wzapp.sitposture.usercenter.repository.UserIncomeRepository;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * 功能: ${DESCRIPTION}<br/>
 *
 * @author qianglong
 * @version 4.8.0
 * @create 2019-08-15 17:25
 **/
@Controller
public class DownloadEndpoint {

    @Autowired
    UserIncomeRepository userIncomeRepository;

    @Autowired
    OrderService orderService;

    /**
     * 测试生成excel表格
     */
    @GetMapping(value = "/download")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{

            Sort sort = new Sort(Sort.Direction.DESC, "createTime");
            List<UserIncome> userIncomeList = userIncomeRepository.findAll(sort);
            //二、 数据转成excel
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/x-download");

            String fileName = "收益记录_" + DateUtil.getDay()+ ".xlsx";
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            // 第一步：定义一个新的工作簿
            XSSFWorkbook wb = new XSSFWorkbook();
            XSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 16);

            // 第二步：创建一个Sheet页
            XSSFSheet sheet = wb.createSheet("收益");
            //设置行高
            sheet.setDefaultRowHeight((short) (2 * 256));
            //设置列宽
            sheet.setColumnWidth(0, 1000);
            sheet.setColumnWidth(1,5500);
            sheet.setColumnWidth(2,5500);
            sheet.setColumnWidth(3,5500);
            sheet.setColumnWidth(4,5500);
            sheet.setColumnWidth(5,10000);

            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = row.createCell(0);
            cell.setCellValue("序号 ");
            cell = row.createCell(1);
            cell.setCellValue("收益人账号 ");
            cell = row.createCell(2);
            cell.setCellValue("收益金额（元）");
            cell = row.createCell(3);
            cell.setCellValue("收益人联系方式");
            cell = row.createCell(4);
            cell.setCellValue("时间");
            cell = row.createCell(5);
            cell.setCellValue("描述");

            XSSFRow rows;
            XSSFCell cells;
            for (int i = 0; i < userIncomeList.size(); i++) {
                UserIncome userIncome = userIncomeList.get(i);
                int index = i+1;
                // 第三步：在这个sheet页里创建一行
                rows = sheet.createRow(index);
                // 第四步：在该行创建一个单元格
                cells = rows.createCell(0);
                // 第五步：在该单元格里设置值
                cells.setCellValue(index);

                cells = rows.createCell(1);
                if(ToolUtil.isNotEmpty(userIncome.getAccount())){
                    cells.setCellValue(userIncome.getAccount());
                }else {
                    cells.setCellValue(userIncome.getMobile());
                }


                cells = rows.createCell(2);
                cells.setCellValue(userIncome.getAmount());

                cells = rows.createCell(3);
                cells.setCellValue(userIncome.getMobile());

                cells = rows.createCell(4);
                //yyyy-MM-dd HH:mm:ss
                cells.setCellValue(DateUtil.getTime(new Date(userIncome.getCreateTime())));

                cells = rows.createCell(5);
                cells.setCellValue(userIncome.getInfo());
            }

            // 第二步：创建一个Sheet页
            XSSFSheet sheet2 = wb.createSheet("付费用户（不含苹果用户）");
            //设置行高
            sheet2.setDefaultRowHeight((short) (2 * 256));
            //设置列宽
            sheet2.setColumnWidth(0, 1000);
            sheet2.setColumnWidth(1,5500);
            sheet2.setColumnWidth(2,5500);
            sheet2.setColumnWidth(3,5500);
            sheet2.setColumnWidth(4,5500);
            sheet2.setColumnWidth(5,5500);
            sheet2.setColumnWidth(6,10000);
            sheet2.setColumnWidth(7,10000);

            XSSFRow row2 = sheet2.createRow(0);
            XSSFCell cell2 = row2.createCell(0);
            cell2.setCellValue("序号 ");
            cell2 = row2.createCell(1);
            cell2.setCellValue("手机号 ");
            cell2 = row2.createCell(2);
            cell2.setCellValue("用户名称 ");
            cell2 = row2.createCell(3);
            cell2.setCellValue("付款金额（元）");
            cell2 = row2.createCell(4);
            cell2.setCellValue("支付方式 ");
            cell2 = row2.createCell(5);
            cell2.setCellValue("订单号 ");
            cell2 = row2.createCell(6);
            cell2.setCellValue("第三方支付交易号 ");
            cell2 = row2.createCell(7);
            cell2.setCellValue("支付时间");

            List<UserOrderDTO> orderDTOList = orderService.findAllPaidUserOrder();
            if(ToolUtil.isNotEmpty(orderDTOList)){
                for (int i = 0; i < orderDTOList.size(); i++) {
                    UserOrderDTO userOrderDTO = orderDTOList.get(i);
                    int index = i+1;
                    // 第三步：在这个sheet页里创建一行
                    rows = sheet2.createRow(index);
                    // 第四步：在该行创建一个单元格
                    cells = rows.createCell(0);
                    // 第五步：在该单元格里设置值
                    cells.setCellValue(index);

                    cells = rows.createCell(1);
                    cells.setCellValue(userOrderDTO.getMobile());

                    cells = rows.createCell(2);
                    if(ToolUtil.isNotEmpty(userOrderDTO.getAccount())){
                        cells.setCellValue(userOrderDTO.getAccount());
                    }else {
                        cells.setCellValue(userOrderDTO.getMobile());
                    }

                    cells = rows.createCell(3);
                    cells.setCellValue(userOrderDTO.getPayAmount());

                    cells = rows.createCell(4);
                    if(userOrderDTO.getPayMethod()==4){
                        cells.setCellValue("苹果购买");
                    }else{
                        cells.setCellValue(PayConst.PAY_METHOD_ALI.equals(userOrderDTO.getPayMethod())?"支付宝":"微信");
                    }

                    cells = rows.createCell(5);
                    cells.setCellValue(userOrderDTO.getOrderCode());

                    cells = rows.createCell(6);
                    cells.setCellValue(userOrderDTO.getPayCode());

                    cells = rows.createCell(7);
                    //yyyy-MM-dd HH:mm:ss
                    cells.setCellValue(DateUtil.getTime(new Date(userOrderDTO.getPayTime())));

                }

            }
            try {
                OutputStream out = response.getOutputStream();
                wb.write(out);
                out.close();
                wb.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}
